/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JDialogIPAccessCfg.java
 *
 * Created on 2009-12-3, 9:10:13
 */

/**
 *
 * @author Administrator
 */

package com.example.demo.hk.ClientDemo;

import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;


/*****************************************************************************
 *类 ：JDialogIPAccessCfg
 *类描述 ：IP接入配置
 ****************************************************************************/
public class JDialogIPAccessCfg extends javax.swing.JDialog {

    static HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;
    private HCNetSDK.NET_DVR_DEVICEINFO_V30 m_strDeviceInfo;//设备信息
    private HCNetSDK.NET_DVR_IPPARACFG  m_strIpparaCfg;//IP参数
    private NativeLong m_lUserID;//用户ID
    
    private int m_iCurChanIndex;
    private int m_iCurSel;
    private boolean m_bCurEnable;//is the channel added or not
    private boolean m_bAnalogChange;
    private boolean m_bGetIpCfg;

    public static final int ANALOG_CHAN_DISABLE = 0;
    public static final int ANALOG_CHAN_ENABLE = 1;
    public static final int IP_CHAN_DISABLE = 2;
    public static final int IP_CHAN_ENABLE = 3;

    /*************************************************
    函数:      JDialogIPAccessCfg
    函数描述:   构造函数   Creates new form JDialogIPAccessCfg
     *************************************************/
    public JDialogIPAccessCfg(java.awt.Frame parent, boolean modal, NativeLong lUserID, HCNetSDK.NET_DVR_DEVICEINFO_V30 strDeviceInfo) {
        super(parent, modal);
        initComponents();

        m_lUserID =lUserID;
        m_strDeviceInfo = strDeviceInfo;
        
        initialDialog();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTableIPAccess = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldIpAddress = new javax.swing.JTextField();
        jTextFieldPort = new javax.swing.JTextField();
        jTextFieldChannel = new javax.swing.JTextField();
        jTextFieldUserName = new javax.swing.JTextField();
        jPasswordFieldPawssword = new javax.swing.JPasswordField();
        jPanel2 = new javax.swing.JPanel();
        jButtonEnable = new javax.swing.JButton();
        jButtonAdd = new javax.swing.JButton();
        jButtonDelete = new javax.swing.JButton();
        jButtonSave = new javax.swing.JButton();
        jButtonRefresh = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jButtonExit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("IP接入配置");

        jTableIPAccess.setModel(this.initialTableModel());
        jTableIPAccess.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jTableIPAccessMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(jTableIPAccess);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("添加通道信息"));

        jLabel1.setText("IP地址");

        jLabel2.setText("端口号");

        jLabel3.setText("通道号");

        jLabel4.setText("用户名");

        jLabel5.setText("密码");

        jTextFieldIpAddress.setText("172.7.160.2");

        jTextFieldPort.setText("8000");

        jTextFieldChannel.setText("1");

        jTextFieldUserName.setText("admin");

        jPasswordFieldPawssword.setText("12345");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldIpAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jTextFieldChannel, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jTextFieldPort, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPasswordFieldPawssword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldIpAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldChannel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jPasswordFieldPawssword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("IP接入配置"));

        jButtonEnable.setText("启用");
        jButtonEnable.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEnableActionPerformed(evt);
            }
        });

        jButtonAdd.setText("添加");
        jButtonAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddActionPerformed(evt);
            }
        });

        jButtonDelete.setText("删除");
        jButtonDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeleteActionPerformed(evt);
            }
        });

        jButtonSave.setText("保存");
        jButtonSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSaveActionPerformed(evt);
            }
        });

        jButtonRefresh.setText("刷新");
        jButtonRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRefreshActionPerformed(evt);
            }
        });

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);

        jButtonExit.setText("退出");
        jButtonExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonExitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButtonAdd)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonDelete))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addComponent(jButtonEnable)))
                .addGap(30, 30, 30)
                .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonRefresh)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonExit))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addComponent(jButtonSave)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jButtonEnable)
                                .addGap(21, 21, 21))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jButtonSave)
                                .addGap(18, 18, 18)))
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButtonDelete)
                                .addComponent(jButtonAdd))
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButtonRefresh)
                                .addComponent(jButtonExit))))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 670, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 507, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

     /*************************************************
    函数:      "启用"  按钮响应函数
    函数描述:   启用或禁用模拟通道
     *************************************************/
    private void jButtonEnableActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonEnableActionPerformed
    {//GEN-HEADEREND:event_jButtonEnableActionPerformed
        if (m_bCurEnable)
        {
            DefaultTableModel IpTableModel = ((DefaultTableModel) jTableIPAccess.getModel());
            IpTableModel.setValueAt("否", jTableIPAccess.getSelectedRow(), 5);
            IpTableModel.setValueAt(ANALOG_CHAN_DISABLE * 1000 + m_iCurChanIndex + "", jTableIPAccess.getSelectedRow(), 6);
            m_strIpparaCfg.byAnalogChanEnable[m_iCurChanIndex] = 0;
            SetCtrlStat(0);
        }
        else
        {
            DefaultTableModel IpTableModel = ((DefaultTableModel) jTableIPAccess.getModel());
            IpTableModel.setValueAt("是", jTableIPAccess.getSelectedRow(), 5);
            IpTableModel.setValueAt(ANALOG_CHAN_ENABLE * 1000 + m_iCurChanIndex + "", jTableIPAccess.getSelectedRow(), 6);
            m_strIpparaCfg.byAnalogChanEnable[m_iCurChanIndex] = 1;
            SetCtrlStat(1);
        }
        m_bAnalogChange = true;
    }//GEN-LAST:event_jButtonEnableActionPerformed

     /*************************************************
    函数:      "添加"  按钮响应函数
    函数描述:   添加或修改IP通道参数
     *************************************************/
    private void jButtonAddActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonAddActionPerformed
    {//GEN-HEADEREND:event_jButtonAddActionPerformed
	int i = 0;
	int j = 0;
	int dwID = 0;
	boolean bExistDev = false;
	int iIPChanIndex = m_iCurChanIndex - HCNetSDK.MAX_ANALOG_CHANNUM;
	if (iIPChanIndex < 0 && iIPChanIndex >= HCNetSDK.MAX_IP_CHANNEL)
	{
               JOptionPane.showMessageDialog(this, String.format("chan [%d] not ip Channel", m_iCurChanIndex));
		return;
	}
	if (m_iCurSel < 0)
	{
		JOptionPane.showMessageDialog(this,"请选择列表中的通道");
		return;
	}
	if (Integer.parseInt(jTextFieldChannel.getText()) == 0 || (new String(jTextFieldIpAddress.getText()).equals("0.0.0.0")))
	{
		JOptionPane.showMessageDialog(this,"参数错误");
		return;
	}

	dwID = m_strIpparaCfg.struIPChanInfo[iIPChanIndex].byIPID;//current connetcted IP device ID
	//this ip channel is connected to devices
        if (dwID > 0)
	{
		for (j = 0; j < HCNetSDK.MAX_IP_CHANNEL; j++)
		{
			if (iIPChanIndex == j)
			{
				continue;
			}
			if (m_strIpparaCfg.struIPChanInfo[j].byIPID == dwID)
			{
				bExistDev = true;
				break;
			}
		}

		if (!bExistDev)
		{
                       //this IP device is not connected in other IP channels, clear its device info
			m_strIpparaCfg.struIPDevInfo[dwID-1] = new HCNetSDK.NET_DVR_IPDEVINFO();
		}
	}
	//clear device info
	m_strIpparaCfg.struIPChanInfo[iIPChanIndex] = new HCNetSDK.NET_DVR_IPCHANINFO();

	bExistDev = false;
	dwID = 0;

	for (i = 0; i < hCNetSDK.MAX_IP_DEVICE; i++)
	{
		//find existed device
                String sIP1 = toIPString(new String(m_strIpparaCfg.struIPDevInfo[i].struIP.sIpV4));//结构体里的设备IP

                String sIP2 =  toIPString(new String(jTextFieldIpAddress.getText()));//界面获取的设备IP
                
		if (sIP2.equals(sIP1) && (Integer.parseInt(jTextFieldPort.getText() ) == m_strIpparaCfg.struIPDevInfo[i].wDVRPort) )
		{
			dwID = i+1;
			System.out.println(String.format("dev[%d]ip[%s]exist", dwID, m_strIpparaCfg.struIPDevInfo[i].struIP.sIpV4));
			for (j = 0; j< HCNetSDK.MAX_IP_CHANNEL; j++)
			{
				if ((m_strIpparaCfg.struIPChanInfo[j].byIPID == dwID) && (Integer.parseInt(jTextFieldChannel.getText()) == m_strIpparaCfg.struIPChanInfo[j].byChannel))
				{
					System.out.println(String.format("ipdev[%d]ip[%s]exist, chan[%d] had added", dwID, m_strIpparaCfg.struIPDevInfo[i].struIP.sIpV4, Integer.parseInt(jTextFieldChannel.getText())));
					JOptionPane.showMessageDialog(this, "对应IP设备的通道已经被添加");
					return;
				}
			}
			bExistDev = true;
                        System.out.println(String.format("ipdev[%d]ip[%s]existed and chan[%d] had not been added", dwID, m_strIpparaCfg.struIPDevInfo[i].struIP.sIpV4, Integer.parseInt(jTextFieldChannel.getText())));
			break;
		}
	}

	if (dwID == 0)//new device
	{
		for (i = 0; i< HCNetSDK.MAX_IP_DEVICE; i++)
		{
			if (m_strIpparaCfg.struIPDevInfo[i].dwEnable != 1)
			{
				dwID = i + 1;//find the first empty node
			        System.out.println(String.format("new ip[%s], ipdev[%d] is disable&can be use",jTextFieldIpAddress.getText(),  dwID));
				break;
			}
		}
	}
	if (dwID == 0)
	{
		JOptionPane.showMessageDialog(this, "超过最大可添加的ip设备数目");
		return;
	}
	m_strIpparaCfg.struIPChanInfo[iIPChanIndex].byEnable = 0;
	m_strIpparaCfg.struIPChanInfo[iIPChanIndex].byChannel = (byte)Integer.parseInt(jTextFieldChannel.getText());
	m_strIpparaCfg.struIPChanInfo[iIPChanIndex].byIPID = (byte)dwID;

	if (!bExistDev)
	{
		m_strIpparaCfg.struIPDevInfo[dwID-1].struIP.sIpV4 =  (jTextFieldIpAddress.getText() + "\0").getBytes();
		m_strIpparaCfg.struIPDevInfo[dwID-1].dwEnable = 1;
		m_strIpparaCfg.struIPDevInfo[dwID-1].sUserName =  (jTextFieldUserName.getText() + "\0").getBytes();
		m_strIpparaCfg.struIPDevInfo[dwID-1].sPassword =  (jPasswordFieldPawssword.getPassword() + "\0").getBytes();
		m_strIpparaCfg.struIPDevInfo[dwID-1].wDVRPort = (short)Integer.parseInt(jTextFieldPort.getText());
	}

        DefaultTableModel IPTableModel = ((DefaultTableModel) jTableIPAccess.getModel());//获取表格模型

        IPTableModel.setValueAt(toIPString(new String(m_strIpparaCfg.struIPDevInfo[dwID - 1].struIP.sIpV4)), m_iCurSel, 2);
        IPTableModel.setValueAt(m_strIpparaCfg.struIPDevInfo[dwID - 1].wDVRPort + "", m_iCurSel, 3);
        IPTableModel.setValueAt(m_strIpparaCfg.struIPChanInfo[iIPChanIndex].byChannel + "", m_iCurSel, 4);
        IPTableModel.setValueAt("是", m_iCurSel, 5);
        IPTableModel.setValueAt((IP_CHAN_ENABLE*1000 + m_iCurChanIndex) + "", m_iCurSel, 6);
        SetCtrlStat(IP_CHAN_ENABLE);
    }//GEN-LAST:event_jButtonAddActionPerformed

     /*************************************************
    函数:      "删除"  按钮响应函数
    函数描述:   删除IP通道
     *************************************************/
    private void jButtonDeleteActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonDeleteActionPerformed
    {//GEN-HEADEREND:event_jButtonDeleteActionPerformed
       	int i = 0;
	int dwID = 0;
	boolean bDeleteDev = true;
	int iIPChan = m_iCurChanIndex - HCNetSDK.MAX_ANALOG_CHANNUM;//Current ip channel array index
	if (iIPChan < 0 && iIPChan >= HCNetSDK.MAX_IP_CHANNEL)
	{
		System.out.println(String.format("chanindex[%d] err ip Channel", m_iCurChanIndex));
		return;
	}
	dwID = m_strIpparaCfg.struIPChanInfo[iIPChan].byIPID;
	if (dwID <= 0)
	{
            System.out.println("the ip device error!");
	   return;
	}
	for (i = 0; i < HCNetSDK.MAX_IP_CHANNEL; i++)
	{
		if (iIPChan != i)
		{
			if (dwID == m_strIpparaCfg.struIPChanInfo[i].byIPID)
			{
				bDeleteDev = false;//canot delete this IP device as it has been connected in other ip channel
                                System.out.println("other enable ip channel use the same ip device");
				break;
			}
		}
	}

	//清空ip设备的信息/clear current ip device
	if (bDeleteDev)
	{
		m_strIpparaCfg.struIPDevInfo[dwID-1] = new HCNetSDK.NET_DVR_IPDEVINFO();
	}
	//清空当前ip通道的信息/clear current ip chan
	m_strIpparaCfg.struIPChanInfo[iIPChan] = new HCNetSDK.NET_DVR_IPCHANINFO();

         DefaultTableModel IPTableModel = ((DefaultTableModel) jTableIPAccess.getModel());//获取表格模型
        IPTableModel.setValueAt("0.0.0.0", m_iCurSel, 2);
        IPTableModel.setValueAt( "0", m_iCurSel, 3);
        IPTableModel.setValueAt("0", m_iCurSel, 4);
        IPTableModel.setValueAt("否", m_iCurSel, 5);
        IPTableModel.setValueAt((IP_CHAN_DISABLE*1000 + m_iCurChanIndex) + "", m_iCurSel, 6);
	SetCtrlStat(IP_CHAN_DISABLE);
    }//GEN-LAST:event_jButtonDeleteActionPerformed

     /*************************************************
    函数:      "保存"  按钮响应函数
    函数描述:   保存参数到设备
     *************************************************/
    private void jButtonSaveActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonSaveActionPerformed
    {//GEN-HEADEREND:event_jButtonSaveActionPerformed
        if (m_bAnalogChange)
        {
            int iResponse = JOptionPane.showConfirmDialog(this, "修改模拟设备配置,需要重启设备,确定重启?", "重启", JOptionPane.OK_CANCEL_OPTION);
            //确认
            if (iResponse != 0)
            {
                return;
            }
        }

        m_strIpparaCfg.write();
        Pointer lpNetConfig = m_strIpparaCfg.getPointer();
        boolean setDVRConfigSuc = hCNetSDK.NET_DVR_SetDVRConfig(m_lUserID, HCNetSDK.NET_DVR_SET_IPPARACFG,
                new NativeLong(0), lpNetConfig, m_strIpparaCfg.size());
        m_strIpparaCfg.read();

        if (setDVRConfigSuc)
        {
            JOptionPane.showMessageDialog(this, "IPC配置设置成功!请刷新!");
        }
        else
        {
            JOptionPane.showMessageDialog(this, "IPC配置设置失败");
        }
    }//GEN-LAST:event_jButtonSaveActionPerformed

    /*************************************************
    函数:      "刷新"  按钮响应函数
    函数描述:   重新获取IP参数并显示
     *************************************************/
    private void jButtonRefreshActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonRefreshActionPerformed
    {//GEN-HEADEREND:event_jButtonRefreshActionPerformed
        initialDialog();
    }//GEN-LAST:event_jButtonRefreshActionPerformed

      /*************************************************
    函数:      "退出"  按钮响应函数
    函数描述:   销毁对话框
     *************************************************/
    private void jButtonExitActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonExitActionPerformed
    {//GEN-HEADEREND:event_jButtonExitActionPerformed
       dispose();
    }//GEN-LAST:event_jButtonExitActionPerformed

     /*************************************************
    函数:      "IP通道配置"  表单击相应函数
    函数描述:   改变相关控制enable属性
     *************************************************/
    private void jTableIPAccessMousePressed(java.awt.event.MouseEvent evt)//GEN-FIRST:event_jTableIPAccessMousePressed
    {//GEN-HEADEREND:event_jTableIPAccessMousePressed
        int iPos =  jTableIPAccess.getSelectedRow();

 	if (iPos < 0)
 	{
 		return;
 	}
 	m_iCurSel = iPos;
        DefaultTableModel IpTableModel = ((DefaultTableModel) jTableIPAccess.getModel());
	int iData = Integer.parseInt(IpTableModel.getValueAt(m_iCurSel, 6).toString());

	m_iCurChanIndex = iData%1000;

	SetCtrlStat(iData/1000);
    }//GEN-LAST:event_jTableIPAccessMousePressed
    
    /*************************************************
    函数:      initialTableModel
    函数描述:  初始化IP接入信息列表,写入列名称
     *************************************************/
    public DefaultTableModel initialTableModel()
    {
        String tabeTile[];
        tabeTile = new String[]{"序号", "通道号", "IP地址", "端口号", "IP通道号", "是否在线" , "ItemData"};
        DefaultTableModel alarmTableModel = new DefaultTableModel(tabeTile, 0);
        return alarmTableModel;
    }

    /*************************************************
    函数:      initialDialog
    函数描述:  初始化Dialog,获取IP配置参数并显示
     *************************************************/
    private void initialDialog()
    {
        ((DefaultTableModel) jTableIPAccess.getModel()).getDataVector().removeAllElements();//搜索前先清空列表

        //还是要再次获取IP接入配置,防止在外部改变配置后无法刷新到设备里的配置
        IntByReference ibrBytesReturned = new IntByReference(0);//获取IP接入配置参数

        m_strIpparaCfg = new HCNetSDK.NET_DVR_IPPARACFG();
        m_strIpparaCfg.write();
        Pointer lpIpParaConfig = m_strIpparaCfg.getPointer();
        m_bGetIpCfg = hCNetSDK.NET_DVR_GetDVRConfig(m_lUserID, HCNetSDK.NET_DVR_GET_IPPARACFG, new NativeLong(0), lpIpParaConfig, m_strIpparaCfg.size(), ibrBytesReturned);
        m_strIpparaCfg.read();

        int i = 0;
        int dwChanNum = 0;
        int iIndex = 0;
        int dwIPChanIndex = 0;
        int dwID = 0;
        m_iCurChanIndex = -1;
        m_iCurSel = -1;

        EnableInfoCtrl(false);
	SetCtrlStat(2);
        DefaultTableModel IPTableModel = ((DefaultTableModel) jTableIPAccess.getModel());//获取表格模型

        if (m_bGetIpCfg)
        {
            for (i = 0; i < hCNetSDK.MAX_CHANNUM_V30; i++)
            {
                Vector<String> newRow = new Vector<String>();
                dwChanNum = i + m_strDeviceInfo.byStartChan;

                if (i < m_strDeviceInfo.byChanNum)
                {
                    newRow.add(String.format("%02d", iIndex + 1));
                    newRow.add("Camera" + dwChanNum);
                    newRow.add("本地");
                    newRow.add("0");
                    newRow.add("0");

                    if (m_strIpparaCfg.byAnalogChanEnable[i] == 1)
                    {
                        newRow.add("是");
                        newRow.add((ANALOG_CHAN_ENABLE*1000 + i) + "");
                    }
                    else
                    {
                        newRow.add("否");
                        newRow.add((ANALOG_CHAN_DISABLE*1000 + i) + "");
                    }
                    iIndex++;
                    IPTableModel.getDataVector().add(newRow);
                    ((DefaultTableModel) jTableIPAccess.getModel()).fireTableStructureChanged();
                }
                else
                {
                    if (i >= HCNetSDK.MAX_ANALOG_CHANNUM && i < (m_strDeviceInfo.byIPChanNum + HCNetSDK.MAX_ANALOG_CHANNUM))
                    {
                        dwIPChanIndex = i - HCNetSDK.MAX_ANALOG_CHANNUM;
                        dwChanNum = dwIPChanIndex + m_strDeviceInfo.byStartChan;
                        dwID = m_strIpparaCfg.struIPChanInfo[dwIPChanIndex].byIPID;

                        newRow.add(String.format("%02d", iIndex + 1));
                        newRow.add("IPCamera" + dwChanNum);

                        if (m_strIpparaCfg.struIPChanInfo[dwIPChanIndex].byIPID != 0)
                        {
                            newRow.add(toIPString(new String(m_strIpparaCfg.struIPDevInfo[dwID - 1].struIP.sIpV4)));
                            newRow.add(m_strIpparaCfg.struIPDevInfo[dwID - 1].wDVRPort + "");
                            newRow.add(m_strIpparaCfg.struIPChanInfo[dwIPChanIndex].byChannel + "");
                            if (m_strIpparaCfg.struIPChanInfo[dwIPChanIndex].byEnable == 1)
                            {
                                newRow.add("在线");
                            }
                            else
                            {
                                newRow.add("离线");
                            }
                        newRow.add((IP_CHAN_ENABLE*1000 + i) + "");
                        }
                         else
                        {
                            newRow.add("0.0.0.0");
                            newRow.add("0");
                            newRow.add("0");
                            newRow.add("否");
                            newRow.add((IP_CHAN_DISABLE*1000 + i) + "");
                        }
                        iIndex++;
                        IPTableModel.getDataVector().add(newRow);
                    ((DefaultTableModel) jTableIPAccess.getModel()).fireTableStructureChanged();
                    }               
                }
            }

		jButtonSave.setEnabled(true);
		this.setEnabled(true);
        }
        else
        {
		jButtonSave.setEnabled(false);
                JOptionPane.showMessageDialog(this, "不支持IP设备");
        }
    }

    /*************************************************
    函数:      EnableInfoCtrl
    函数描述:  修改enable属性
     *************************************************/
   private void EnableInfoCtrl(boolean bEnable)
{
	jTextFieldIpAddress.setEditable(bEnable);
	jTextFieldUserName.setEditable(bEnable);
	jTextFieldPort.setEditable(bEnable);
	jPasswordFieldPawssword.setEditable(bEnable);
	jTextFieldChannel.setEditable(bEnable);
}

    /*************************************************
    函数:      SetCtrlStat
    函数描述:  更爱按钮名称及enable属性
     *************************************************/
    private void SetCtrlStat(int iStat)
    {
        int dwID = 0;
        switch (iStat)
        {
            case ANALOG_CHAN_DISABLE://analog disable
                jButtonEnable.setEnabled(true);
                jButtonAdd.setEnabled(false);
                jButtonDelete.setEnabled(false);
                jButtonEnable.setText("启用");
                EnableInfoCtrl(false);
                m_bCurEnable = false;
                break;
            case ANALOG_CHAN_ENABLE://analog enable channel
                jButtonEnable.setEnabled(true);
                jButtonAdd.setEnabled(false);
                jButtonDelete.setEnabled(false);
                jButtonEnable.setText("禁用");
                EnableInfoCtrl(false);
                m_bCurEnable = true;
                break;
            case IP_CHAN_DISABLE://digital disable channel
                jButtonAdd.setEnabled(true);
                jButtonAdd.setText("添加");
                jButtonEnable.setEnabled(false);
                jButtonDelete.setEnabled(false);
                EnableInfoCtrl(true);
                m_bCurEnable = false;
                break;
            case IP_CHAN_ENABLE://digital enable
                jButtonAdd.setEnabled(true);
                jButtonAdd.setText("修改");
                jButtonEnable.setEnabled(false);
                jButtonDelete.setEnabled(true);
                if ((m_iCurChanIndex - HCNetSDK.MAX_ANALOG_CHANNUM) != 0)
                {
                    dwID = m_strIpparaCfg.struIPChanInfo[m_iCurChanIndex - HCNetSDK.MAX_ANALOG_CHANNUM].byIPID;
                    if (dwID > 0)
                    {
                        jTextFieldIpAddress.setText(new String(m_strIpparaCfg.struIPDevInfo[dwID - 1].struIP.sIpV4));
                    }
                }
                EnableInfoCtrl(true);
                m_bCurEnable = true;
                break;
            default:
                break;
        }
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAdd;
    private javax.swing.JButton jButtonDelete;
    private javax.swing.JButton jButtonEnable;
    private javax.swing.JButton jButtonExit;
    private javax.swing.JButton jButtonRefresh;
    private javax.swing.JButton jButtonSave;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPasswordField jPasswordFieldPawssword;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTableIPAccess;
    private javax.swing.JTextField jTextFieldChannel;
    private javax.swing.JTextField jTextFieldIpAddress;
    private javax.swing.JTextField jTextFieldPort;
    private javax.swing.JTextField jTextFieldUserName;
    // End of variables declaration//GEN-END:variables

    /*************************************************
    函数:      toIPString
    函数描述:  主要是与C中的字符串格式一致,比较字符串时,去"\0"之前的一段字符串
     *************************************************/
     String toIPString(String s)
    {
         String[] sIP = new String[2];//结构体里的设备IP
         sIP = s.split("\0", 2);
         return sIP[0];
    }

}
